package com.ruoyi.shanghuliebiao2.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.shanghuliebiao2.domain.CheDrawal;
import com.ruoyi.shanghuliebiao2.domain.CheDrawalStatement;
import com.ruoyi.shanghuliebiao2.service.ICheDrawalService;
import com.ruoyi.shanghuliebiao2.service.ICheDrawalStatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.shanghuliebiao2.domain.CheStatement;
import com.ruoyi.shanghuliebiao2.service.ICheStatementService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 收支明细Controller
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/statement")
public class CheStatementController extends BaseController
{
    @Autowired
    private ICheStatementService cheStatementService;
    @Autowired
    private ICheDrawalService cheDrawalService;

    @Autowired
    private ICheDrawalStatementService idrawalStatementService;

    /**
     * 查询收支明细列表
     */
    @RequiresPermissions("shanghuliebiao2:statement:list")
    @GetMapping("/list")
    public TableDataInfo list(CheStatement cheStatement)
    {

        startPage();
        List<CheStatement> list = cheStatementService.selectCheStatementList(cheStatement);
        for (CheStatement ss : list){
             Long drawalId = ss.getDrawalId();
            CheDrawalStatement drawalStatement=new CheDrawalStatement();
            drawalStatement.setStatementId(drawalId);
            List<CheDrawalStatement> drawalStatementList = idrawalStatementService.selectCheDrawalStatementList(drawalStatement);
            List drawal = new ArrayList();
            for (CheDrawalStatement dd : drawalStatementList){
                 Long drawalId1 = dd.getDrawalId();
                CheDrawal cheDrawal = cheDrawalService.selectCheDrawalById(drawalId1);
                drawal.add(cheDrawal);
            }
            ss.setDrawal(drawal);

        }
        return getDataTable(list);
    }

    /**
     * 导出收支明细列表
     */
    @RequiresPermissions("shanghuliebiao2:statement:export")
    @Log(title = "收支明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheStatement cheStatement)
    {
        List<CheStatement> list = cheStatementService.selectCheStatementList(cheStatement);
        ExcelUtil<CheStatement> util = new ExcelUtil<CheStatement>(CheStatement.class);
        util.exportExcel(response, list, "收支明细数据");
    }

    /**
     * 获取收支明细详细信息.
     */
    @RequiresPermissions("shanghuliebiao2:statement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheStatementService.selectCheStatementById(id));
    }

    /**
     * 新增收支明细
     */
    @RequiresPermissions("shanghuliebiao2:statement:add")
    @Log(title = "收支明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheStatement cheStatement)
    {

        return toAjax(cheStatementService.insertCheStatement(cheStatement));
    }

    /**
     * 修改收支明细
     */
    @RequiresPermissions("shanghuliebiao2:statement:edit")
    @Log(title = "收支明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheStatement cheStatement)
    {
        return toAjax(cheStatementService.updateCheStatement(cheStatement));
    }

    /**
     * 删除收支明细
     */
    @RequiresPermissions("shanghuliebiao2:statement:remove")
    @Log(title = "收支明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheStatementService.deleteCheStatementByIds(ids));
    }
}
